package com.symaster.common.test.memory;

import com.symaster.common.swing.util.SwingUtil;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Random;

/**
 * @author yinmiao
 * @version 2023/11/30
 */
public class App1 {

    public static void main(String[] args) {
        int arrayLen = 10000;
        Random random = new Random();

        TestObj testObj = new TestObj();
        testObj.ints = new int[arrayLen];
        for (int i = 0; i < testObj.ints.length; i++) {
            testObj.ints[i] = random.nextInt(99999);
        }
        System.out.println(Arrays.toString(testObj.ints));

        JButton button1 = new JButton("随机旧值");
        button1.addActionListener(e -> {
            for (int i = 0; i < testObj.ints.length; i++) {
                testObj.ints[i] = random.nextInt(99999);
            }
            System.out.println(Arrays.toString(testObj.ints));
        });

        JButton button2 = new JButton("新建");
        button2.addActionListener(e -> {
            testObj.ints = new int[1000];
            for (int i = 0; i < testObj.ints.length; i++) {
                testObj.ints[i] = random.nextInt(arrayLen);
            }
            System.out.println(Arrays.toString(testObj.ints));
        });

        JButton button3 = new JButton("GC");
        button3.addActionListener(e -> {
            System.gc();
        });

        JButton button4 = new JButton("释放");
        button4.addActionListener(e -> {
            testObj.ints = null;
            System.gc();
        });

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new FlowLayout());
        jPanel.add(button1);
        jPanel.add(button2);
        jPanel.add(button3);
        jPanel.add(button4);

        JFrame jFrame = new JFrame("App1");
        jFrame.setContentPane(jPanel);
        jFrame.setPreferredSize(new Dimension(600, 900));
        jFrame.pack();
        jFrame.setVisible(true);
        SwingUtil.toCenter(jFrame);

    }

    private static class TestObj {
        public int[] ints;
    }
}
